package com.qk.management.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Just、倾城
 * @created 2025-10-09
 * @description
 *
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper rolemapper;


    @Override
    public void update(Role role) {
        // 参数校验
        if(BeanUtil.hasNullField(role,"remark","createTime","updateTime")){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        rolemapper.update(role);
    }
    @Override
    public Role getById(Integer id) {
        Role role = rolemapper.selectById(id);
        return role;
    }



    @Override
    public void add(Role role) {
        //参数校验
        if(BeanUtil.hasNullField(role,"id","name","label",  "remark","createTime","updateTime")){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        rolemapper.add(role);
    }



    @Override
    public void deleteById(Integer id) {
        //参数校验 无
        rolemapper.deleteById(id);
    }

    @Override
    public PageResult<Role> roleList(RoleDTO roleDTO) {
        //数据校验 无
        //开始分页
        PageHelper.startPage(roleDTO.getPage(), roleDTO.getPageSize());
        //执行查询
        List<Role> list = rolemapper.selectByPage(roleDTO);
        //获取信息
        Page<Role> pageInfo = (Page<Role>) list;
        return PageResult.<Role>builder()
                        .total(Integer.getInteger(pageInfo.getTotal()+""))
                        .rows(pageInfo.getResult())
                        .build();
    }



    @Override
    public List<Role> list() {
        List<Role> list  = rolemapper.select();
        return list;
    }


}
